package TUILayer;//makes this class a part of the TUILayer package
import java.util.Scanner;
import ControlLayer.*;//The TUILayer needs to know the ControlLayer
import ModelLayer.*;

/**
 * User interface class to the AddressBook
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class AddressBookUI
{   
    private AddressCtr addrCtr;//reference to the ControlLayer
    
    //constructor which does nothing
    public AddressBookUI()
    {
        addrCtr = new AddressCtr();
    }
    
    public void start()
    {
        addressMenu();
    }
    
    public void addressMenu()
    {
       boolean exit = false;
       while(!exit)//that is: while exit is false
       {
          int choise = writeAddressBookMenu(); 
          if(choise == 1)
          {   //show person
              getPerson();
          }
          else{
              if(choise == 2){
                    createPerson();     
              }  
              else{
                  if(choise == 3){
                      deletePerson();
                  }  
                  else{
                      if(choise == 4){
                          updatePerson();
                      }
                      else{
                          if(choise == 5){
                              listAllPersons();
                            }
                          else{
                              exit = true;
                           }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
            
       }//end while
    }
    
    public int writeAddressBookMenu()
    {      // makes an object keyboard to read input from the screen
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\n *** AddressBook Menu ***");
        System.out.println("\n (1) Show information about person");
        System.out.println(" (2) Add person");
        System.out.println(" (3) Delete person");
        System.out.println(" (4) Update person");
        System.out.println(" (5) List all persons");
        System.out.println(" (6) Return");
        System.out.print("\n Make your choice: ");
        int choise = keyboard.nextInt();
        return choise;
    }
    
    /**
     * Print a text and takes input from keyboard
     * @param text Print text
     * @return str Return the input from the keyboard 
     */
    private String inputtext(String text)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print(text + ": ");
        String str = keyboard.nextLine();
        return str;
    }
    
    /**
     * Print a text and takes input from keyboard
     * @param text Print text
     * @return no Return the input from the keyboard as a long
     */
    private long inputnumber(String text)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print(text + ": ");
        String str = keyboard.nextLine();
        long no = Long.parseLong(str.trim());  // convert String 2 long
        return no;
    }
    
    public void showId(long id)
    {
        System.out.println("ID of the person: " + id);
    }
    
    public void showName(String name)
    { 
        System.out.println("The name of the person: " + name);
    }
    
    public void showAddress(String address)
    { 
        System.out.println("The address of the person: " + address);
    }
    
    /**
     * Print a person
     * Takes a input fron keyboard: id
     * Call addrCtr.GetPerson() with th id
     * Return a object of the person
     */
    public void getPerson()
    {
        long id = inputnumber("Write Id");
       // Person p = (addrCtr.getPerson(id));
       // String name = p.getName();
       // showName(name);
       // String address = p.getAddress();
       // showAddress(address);
       // return p;
       addrCtr.listPerson(id);
    }
    
    /**
     *  Create a new person
     *  Takes a input fron keyboard: id, name, adress, postalcode, city, tlf
     *  Call addrCtr.createPerson() with the data
     */
    public void createPerson()
    {
        long id = inputnumber("Write Id");
        String name = inputtext("Write Name");
        String address = inputtext("Write Address");
        String postalcode = inputtext("Write Postal Code");
        String city = inputtext("Write City");
        String tlf = inputtext("Write Phone Number");
        addrCtr.createPerson(id, name, address, postalcode, city, tlf);
        System.out.println(" ");
    }
    
    /**
     * Delete a person
     * Takes a input from keyboard: id
     * Call addrCtr.deletePerson() with id
     * Print a message
     */
    public void deletePerson()
    {
        long id = inputnumber("Write Id");
        addrCtr.deletePerson(id);
        System.out.println("Person has been deleted");
        System.out.println(" ");
    }
    
    /**
     * Update a person
     * Takes a input fron keyboard: id, name, adress, postalcode, city, tlf
     * Call addrCtr.updatePerson with the data
     */
    public void updatePerson()
    {
        long id = inputnumber("Write Id");
        String name = inputtext("Write Name");
        String address = inputtext("Write Address");
        String postalcode = inputtext("Write Postal Code");
        String city = inputtext("Write City");
        String tlf = inputtext("Write Phone Number");
        addrCtr.updatePerson(id, name, address, postalcode, city, tlf);
        System.out.println(" ");
    }
    
    /**
     * Print a list of all persons
     */
    public void listAllPersons()
    {           
        addrCtr.listAllPersons();
        System.out.println(" ");
    }
}
            
        
